//
//  SLCAAnimationGroupController.m
//  Objective-C
//
//  Created by S_Line on 2018/7/2.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAAnimationGroupController.h"

@interface SLCAAnimationGroupController ()<CAAnimationDelegate>

@property (nonatomic, weak) UIView *containerView;
@property (nonatomic, weak) CALayer *colorLayer;

@end

@implementation SLCAAnimationGroupController

- (void)initNavBar {
    self.navTitle = @"AnimationGroup";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, kScreenWidth, kScreenHeight - kNavigationBarHeight)];
    self.containerView = containerView;
    [self.view addSubview:containerView];
    
    // create a path
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:CGPointMake(0, 150)];
    [bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)];
    
    // draw the path using a CAShapeLayer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    shapeLayer.lineWidth = 1;
    [self.containerView.layer addSublayer:shapeLayer];
    
    // add a colored layer
    CALayer *colorLayer = [CALayer layer];
    self.colorLayer = colorLayer;
    colorLayer.frame = CGRectMake(0, 0, 32, 32);
    colorLayer.position = CGPointMake(0, 150);
    colorLayer.backgroundColor = [UIColor greenColor].CGColor;
    [self.containerView.layer addSublayer:colorLayer];
    
    UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 0, 100, 40)];
    [startButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [startButton setTitle:@"Start" forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(startButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:startButton];
    
    UIButton *endButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 0, 100, 40)];
    [endButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [endButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [endButton setTitle:@"End" forState:UIControlStateNormal];
    [endButton addTarget:self action:@selector(endButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:endButton];
    
}

- (void)startButtonClick:(UIButton *)sender {
    
    // create a path
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:CGPointMake(0, 150)];
    [bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)];
    
    // create the position animation
    CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animation];
    animation1.keyPath = @"position";
    animation1.path = bezierPath.CGPath;
    animation1.rotationMode = kCAAnimationRotateAuto;
    // create the color animation
    CABasicAnimation *animation2 = [CABasicAnimation animation];
    animation2.keyPath = @"backgroundColor";
    animation2.toValue = (__bridge id)[UIColor redColor].CGColor;
    //create group animation
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    self.colorLayer.position = CGPointMake(300, 150);
    animationGroup.animations = @[animation1, animation2];
    animationGroup.duration = 4.0;
    animationGroup.delegate = self;
    // add the animation to the color layer
    [self.colorLayer addAnimation:animationGroup forKey:@"abc"];
}

- (void)endButtonClick:(UIButton *)sender {
    [self.colorLayer removeAnimationForKey:@"abc"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    // log that the animation stopped
    NSLog(@"The animation stopped (finished: %@)", flag ? @"YES" : @"NO");
}

@end
